package com.chj.comp.common.gateway.filter;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;

/**
 * @author ：cuihui
 * @date ：Created in 2023/2/22
 * @description：自定义私有过滤器（需要在网关路由中配置）
 * @modified By：
 */
@Slf4j
@Component
public class CustomPrivateFilter extends AbstractGatewayFilterFactory {

    @Override
    public GatewayFilter apply(Object config) {
        // grab configuration from Config object
        return (exchange, chain) -> {
            //If you want to build a "pre" filter you need to manipulate the
            //request before calling chain.filter
            ServerHttpRequest.Builder builder = exchange.getRequest().mutate();
            //use builder to manipulate the request
            log.info("-----CustomPrivateFilter-----");
            ServerHttpRequest request = exchange.getRequest();
            String requestPath = request.getPath().value();
            log.info("requestPath-->" + requestPath);
            ServerHttpResponse response = exchange.getResponse();
            HttpStatus statusCode = response.getStatusCode();
            log.info("statusCode-->" + statusCode);
            return chain.filter(exchange.mutate().request(builder.build()).build());
        };
    }

}
